#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 28 12:37:27 2018
@project: 天池比赛-A股主板上市公司公告信息抽取
@group: MZH_314
@author: LHQ
"""


class ZjcTextSlot:
    
    LEVEL_1 = 1 
    LEVEL_2 = 2 
    LEVEL_3 = 3
    LEVEL_NULL = 10

    def __init__(self):
        self.field2level = {"股东全称": self.LEVEL_1,
                           "变动截止日期": self.LEVEL_1,
                           "变动数量": self.LEVEL_2,
                           "变动价格": self.LEVEL_2,
                           "变动后持股数": self.LEVEL_3,
                           "变动后持股比例": self.LEVEL_3,
                           }
        self.fields = tuple(self.field2level.keys())
        self.slots = [None for _ in self.fields]
        self.cur_level = self.LEVEL_1
        
        self.data = []
        
    def clear_slot(self):
        self.slots = [None for _ in self.fields]

    @property
    def next_level(self):
        if self.cur_level == self.LEVEL_1:
            return self.LEVEL_2
        elif self.cur_level == self.LEVEL_2:
            return self.LEVEL_3
        else:
            return self.LEVEL_NULL
   
    def is_empty(self, level):
        indexes = (self.fields.index(f) for f, lv in self.field2level.items() if lv == level)
        not_empty = any([self.slots[i] for i in indexes])
        return not not_empty
    
    def get_level(self, field):
        level = self.field2level.get(field, self.LEVEL_NULL)
        return level
       
    def feed_one(self, entity, field):
        level = self.get_level(field)
        index = self.fields.index(field)
        if level == self.cur_level:
            self.slots[index] = entity
        elif level == self.next_level and not self.is_empty(self.cur_level):
            self.cur_level = level
            self.slots[index] = entity
        elif level == self.LEVEL_1 and self.cur_level != self.LEVEL_1:
            self.commit_and_clear_slots()
            self.cur_level = level
            self.slots[index] = entity
        else:
            pass
        
    def feed_sequence(self, sq):
        for entity, field in sq:
            self.feed_one(entity, field)
        self.commit_and_clear_slots()
        
    def commit_and_clear_slots(self):
        if self.is_empty(self.LEVEL_1):
            pass
        else:
            item = dict(zip(self.fields, self.slots))
            if item["股东全称"] is None and len(self.data) > 0:
                item["股东全称"] = self.data[-1]["股东全称"]
            self.data.append(item)
            self.cur_level = self.LEVEL_1
            self.slots = [None for _ in self.fields]
        
    def get_data(self):
        self.commit_and_clear_slots()
        return self.data[:]
    
    def reset(self):
        self.slots = [None for _ in self.fields]
        self.cur_level = self.LEVEL_1
        self.data.clear()        


if __name__ == "__main__":
    sq= [(0, 10, '2014-05-30', '变动截止日期'),
         (20, 32, '彩虹集团电子股份有限公司', '股东全称'),
         (52, 62, '2014-05-28', '变动截止日期'),
         (93, 100, '4000000', '变动数量')]
    
    slot = ZjcTextSlot()
    for a in sq:
        slot.feed_one(a[2], a[3])
    item = slot.get_data()
    print("1.", item)
    slot.reset()
    slot.feed_sequence([q[2:4] for q in sq])
    item2 = slot.get_data()
    print("2.", item2) 
    
    
    
    